/**
 * Created by Xiaoming on 2018/1/3.
 */
angular.module('module.table.operation')
    .controller('TableNumMealCodeController', TableNumMealCodeController);
TableNumMealCodeController.$inject = ['$state', '$localStorage', '$stateParams', 'TableNumService', 'FileUpload'];
function TableNumMealCodeController($state, $localStorage, $stateParams, TableNumService, FileUpload) {
    var vm = this;
    vm.uploadLogo = uploadLogo; // 点击上传图片
    vm.submitCode = submitCode;
    var tableid = $stateParams.tableid;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.editChange = editChange;
    vm.remove = remove;

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    var shopid = $localStorage[environmental + "shopid"];
    vm.edit = false;
    var qrcodebox = document.getElementById("qrcodebox");
    var qrcodebox1 = document.getElementById("qrcodebox1");
    var setQrcode = new QRCode(qrcodebox, {
        width: 200, // 设置宽高
        height: 200
    });
    var setQrcode1 = new QRCode(qrcodebox1, {
        width: 200, // 设置宽高
        height: 200
    });

    TableNumService.getShopTable(tableid, function (shopTable) {
        vm.shopTable = shopTable;
        if (shopTable.orderimg) {
            setQrcode.makeCode(shopTable.orderimg);
            setQrcode1.makeCode(shopTable.orderimg);
        }
    }, function (error) {
        console.log(error);
    });
    vm.codeType = 2;
    vm.shopTable = {};

    // 上传图片
    function uploadLogo(file) {
        if (!file) {
            return;
        }
        vm.progressPercentage = 0;
        var fr = new FileReader();
        fr.readAsDataURL(file);
        fr.onload = function (e) {
            vm.orderimg = e.target.result;
        };
        FileUpload.uploadFileToUrl(file, false, progressTest).then(function (data) {
            if (data.data.faddress) {

                // 通过图片地址解析二维码
                FileUpload.qrDecode(data.data.faddress, function (data) {
                    vm.qrcodetxt = data.content;
                }, function (error) {
                    vm.orderimg = "";
                    vm.qrcodetxt = "";
                    vm.messageVisible = true;
                    vm.message = "二维码解析失败请确认图片是否正确！";
                })
            }
        }, function (error) {
            console.log(error);
        });
    }

    function submitCode() {
        if (!vm.qrcodetxt || vm.qrcodetxt == "") {
            vm.messageVisible = true;
            vm.message = "请先上传二维码！";
            return;
        }
        updateShopTable();
    }

    function updateShopTable() {
        vm.shopTable.shopid = shopid;
        vm.shopTable.tableid = tableid;
        vm.shopTable.orderimg = vm.qrcodetxt;
        TableNumService.updateShopTable(vm.shopTable, function (data) {
            vm.editChange();
            setQrcode.makeCode(vm.qrcodetxt);
            vm.messageVisible = true;
            vm.message = "修改成功！";
        }, function () {
            vm.messageVisible = true;
            vm.message = "修改失败！";
        })
    }

    function editChange() {
        vm.edit = !vm.edit;
    }

    // 进度条
    function progressTest(evt) {
        vm.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        $(".progress-bar").css({width: vm.progressPercentage + "%"});
    }

    // 移除桌台二维码
    function remove() {
        var r = confirm("确认删除桌台二维码？");
        if (r == false) {
            return;
        }
        vm.shopTable.orderimg = "";
        TableNumService.updateShopTable(vm.shopTable, function (data) {
            vm.editChange();
            $(qrcodebox).find("img").remove();
            $(qrcodebox).find("canvas").remove();
            $(qrcodebox1).find("img").remove();
            $(qrcodebox1).find("canvas").remove();
            vm.messageVisible = true;
            vm.message = "删除成功！";
        }, function () {
            vm.messageVisible = true;
            vm.message = "删除失败！";
        })
    }
}